import {
	login
} from '@/api/api_user'
const state = {
	token: null,
	userInfo: {}
}

const mutations = {
	SET_TOKEN: (state, value) => {
		state.token = value
		console.log(value);
	},
	
	SET_USERINFO: (state, userInfo) => {
		state.userInfo = userInfo
	}
}

const actions = {
	login({
		commit
	}, userInfo) {
		const {
			phone,
			password
		} = userInfo
		return new Promise((resolve, reject) => {
			login({
				phone: phone.trim(),
				password: password
			}).then(response => {
				const {
					token,
					user
				} = response.data
				commit('SET_TOKEN', token)
				commit('SET_USERINFO', user)
				// setToken(data.token)
				resolve()
			}).catch(error => {
				reject(error)
			})
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
